package com.engineering.elink.hessian.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.dao.PersonDynamicMapper;
import com.engineering.elink.domain.PersonDynamic;
import com.engineering.elink.hessian.PersonDynamicHessianService;
@Service
public class PersonDynamicHessianServiceImpl implements PersonDynamicHessianService{

	@Autowired
	private PersonDynamicMapper personDynamicMapper;
	
	@Override
	public  boolean addPersonDynamic(int pdType, int pdFormatId,String pdTitle, String pdContent, String userId, String fkId, String byUserId) {
		PersonDynamic personDynamicInSql = personDynamicMapper.selectByFkIdAndPdType(fkId, pdType,userId);
		if (personDynamicInSql == null) {
			PersonDynamic pd = new PersonDynamic();
			pd.setCreateTime(new Date());
			pd.setFkId(fkId);
			pd.setPdContent(pdContent);
			pd.setPdFormatId(pdFormatId);
			pd.setPdTitle(pdTitle);
			pd.setPdType(pdType);
			pd.setUserId(userId);
			pd.setByUserId(byUserId);
			personDynamicMapper.insertSelective(pd);
			return true;
		}
		return false;
	}

	@Override
	public boolean removePersonDynamic(int pdType, String fkId,String userId) {
		PersonDynamic personDynamic = personDynamicMapper.selectByFkIdAndPdType(fkId, pdType,userId);
		if (personDynamic != null) {
			personDynamicMapper.deleteByPrimaryKey(personDynamic.getPdId());
			return true;
		}
		return false;
	}

	@Override
	public boolean modifyPersonDynamic(int pdType, int pdFormatId,
			String pdTitle, String pdContent, String fkId,String userId) {
		PersonDynamic personDynamicInSql = personDynamicMapper.selectByFkIdAndPdType(fkId, pdType,userId);
		if (personDynamicInSql != null) {
			PersonDynamic personDynamic = new PersonDynamic();
			personDynamic.setCreateTime(new Date());
			personDynamic.setPdContent(pdContent);
			personDynamic.setPdFormatId(pdFormatId);
			personDynamic.setPdTitle(pdTitle);
			personDynamic.setPdType(pdType);
			personDynamic.setPdId(personDynamicInSql.getPdId());
			personDynamicMapper.updateByPrimaryKeySelective(personDynamic);
			return true;
		}
		return false;
	}
	
}
